package github.ziwukun.jsonql.lang;

import cn.hutool.core.util.StrUtil;

/**
 * JSON QL 实现类
 * @author Hugo.Yu
 */
public class JSONQLImpl implements JSONQL{
	private static final long serialVersionUID = -1826130147003098516L;
	Select select;
	String from;
	Where where;
	Limit limit;
	Sort sort;
	
	JSONQLImpl(Select select, String from, Where where, Limit limit, Sort sort) {
		super();
		this.select = select;
		this.from = from;
		this.where = where;
		this.limit = limit;
		this.sort = sort;
		
		asserts();
	}

	@Override
	public Select select() {
		return select;
	}

	@Override
	public String from() {
		return from;
	}

	@Override
	public Where where() {
		return where;
	}

	@Override
	public Limit limit() {
		return limit;
	}

	@Override
	public Sort sort() {
		return sort;
	}

	@Override
	public String toJSONQL() {
		return JSONQLJson.writeJson(this);
	}
	
	private void asserts() {
		if(select == null) {
			throw new JSONQLBuildException("select语句不能为空");
		}
		if(StrUtil.isBlank(from)) {
			throw new JSONQLBuildException("from语句不能为空");
		}
	}

	@Override
	public JSONQL repSelect(Select select) {
		return new JSONQLImpl(select, from, where, limit, sort);
	}

	@Override
	public JSONQL repFrom(String from) {
		return new JSONQLImpl(select, from, where, limit, sort);
	}

	@Override
	public JSONQL repWhere(Where where) {
		return new JSONQLImpl(select, from, where, limit, sort);
	}

	@Override
	public JSONQL repLimit(Limit limit) {
		return new JSONQLImpl(select, from, where, limit, sort);
	}

	@Override
	public JSONQL repSort(Sort sort) {
		return new JSONQLImpl(select, from, where, limit, sort);
	}
}
